package com.yt.core;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by liujunshi on 16/1/3.
 */
public class MsgBean {
    private String errdesc;
    private String errno;
    private Map dto;

    public MsgBean() {
        this.errdesc = "";
        this.errno = "0";
        this.dto = new HashMap();
        this.dto.put("errdesc", this.errdesc);
        this.dto.put("errno", this.errno);
        this.dto.put("timestamp", Long.valueOf((new Date()).getTime() / 1000L));
    }

    public MsgBean(String errno, String errdesc) {
        this.errdesc = errdesc;
        this.errno = errno;
        this.dto = new HashMap();
        this.dto.put("errdesc", errdesc);
        this.dto.put("errno", this.errno);
        this.dto.put("timestamp", Long.valueOf((new Date()).getTime() / 1000L));
    }

    public void put(String key, Object value) {
        this.dto.put(key, value);
    }

    public void putAll(Map msg) {
        if(msg != null && msg.size() > 0) {
            this.dto.putAll(msg);
        }

    }

    public void setFailure(String errorMsg) {
        this.dto.put("errno", Integer.valueOf(1));
        if(errorMsg != null && !errorMsg.contains("dubbo")) {
            this.dto.put("errdesc", errorMsg);
        } else {
            this.dto.put("errdesc", "网络繁忙，请重试");
        }

    }

    public void setFailure(String errno, String errorMsg) {
        this.dto.put("errno", errno);
        if(errorMsg != null && !errorMsg.contains("dubbo") && !errorMsg.contains("Exception")) {
            this.dto.put("errdesc", errorMsg);
        } else {
            this.dto.put("errdesc", "网络繁忙，请重试");
        }

    }

    public void setFailure(int errno, String errorMsg) {
        this.setFailure(String.valueOf(errno), errorMsg);
    }

    public MsgBean failure(String errorMsg) {
        this.setFailure("1", errorMsg);
        return this;
    }

    public Map returnMsg() {
        return this.dto;
    }

    static class AjaxReturn {
        public static final String SUCCESS = "success";
        public static final String STATUS_CODE = "errno";
        public static final String STATUS_INFO = "errdesc";

        AjaxReturn() {
        }
    }
}
